#!/usr/bin/python
# -*- coding: utf-8 -*-
# encoding: utf-8
# Filename: code2html.py

import sys

SYMBOL_TABLE = {
    '&'  : '&amp;',
    '<'  : '&lt;',
    '>'  : '&gt;',
    '\t' : '    '
    }

def CodeToHtml(fi, out):
    try:
        fo = open(out,'w')
    except IOError:
        print 'output file error!'
    else:
        for line in fi:
            for key in SYMBOL_TABLE.keys():
                line = line.replace(key, SYMBOL_TABLE[key])
            fo.write(line)
        fo.close()

if len(sys.argv) == 1:
    try:
        fi = open('code.txt', 'r')
    except IOError:
        print 'cannot open file code.txt'
    else:
        CodeToHtml(fi, 'html.txt')
        fi.close()
elif len(sys.argv) <= 3:
    try:
        fi = open(sys.argv[1], 'r')
    except IOError:
        print 'cannot open file ', sys.argv[1]
    if len(sys.argv) == 2:
        fo = fi.name + '_thml.txt'
    else:
        fo = sys.argv[2]
    CodeToHtml(fi, fo)
    fi.close()
else:
    print"too many arguments"
raw_input('press any key to exit')
